package com.example.protobufdemo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.protobuf.InvalidProtocolBufferException;

import proto.ProductInfoPro;
import proto.ProductInfoPro.ProduceName;
import proto.ProductInfoPro.Product;
import proto.ProductInfoPro.ProductPic;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity {

	Product productParse = null;
	
	Map<Integer, String> picList = new HashMap<>();
	Map<Integer, String> testList = new HashMap<>();
	Map<Integer, Integer> proNumList = new HashMap<>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//添加数据
		testPro();
		
		//取数据
		String luckNum = productParse.getLuckNum();
		String productCode = productParse.getProductCode();
		String amount = productParse.getAmount();
		String phoneNum = productParse.getPhoneNum();
		int uid = productParse.getUid();
		
		TextView tView = (TextView) findViewById(R.id.tv);
		tView.setText(luckNum+productCode+amount+phoneNum+uid);
		
		for (int i = 0; i < 5; i++) {
			ProductPic pic = productParse.getProductPicList(i);
			
			String picURL = pic.getProductPicUrl();
			String proTEST = pic.getProductTest();
			int proNUM = pic.getProductNum();
			
			Log.d("propicList", "picURL:"+picURL+",proTEST:"+proTEST+",proNUM:"+proNUM);
			picList.put(i, picURL);
			testList.put(i, proTEST);
			proNumList.put(i, proNUM);
			
			
			for (int j = 0; j < 5; j++) {
				ProduceName name = pic.getProNameList(j);
				String proName = name.getProName();
				Log.d("propicList", "proName:"+proName);
			}
			
			
		}
		
		
		
	}

	//测试代码
	private void testPro(){
		ProductInfoPro.Product.Builder builder = ProductInfoPro.Product.newBuilder();
		builder.setLuckNum("luckNum11");
		builder.setProductCode("productCode11");
		builder.setAmount("amount11");
		builder.setPhoneNum("phoneNum11");
		builder.setUid(11);
		
		List<ProductPic> picList = new ArrayList<>();
		
		for (int i = 0; i < 5; i++) {
			ProductPic.Builder picBuilder = ProductInfoPro.ProductPic.newBuilder();
			picBuilder.setProductPicUrl("picUrl:"+i);
			picBuilder.setProductTest("test:"+i);
			picBuilder.setProductNum(i);
			
			
			List<ProduceName> nameList = new ArrayList<>();
			for (int j = 0; j < 5; j++) {
				ProduceName.Builder nameBuilder = ProductInfoPro.ProduceName.newBuilder();
				nameBuilder.setProName("aa:"+j);
				ProduceName name = nameBuilder.build();
				nameList.add(name);
			}
			picBuilder.addAllProNameList(nameList);
				
			
			ProductPic pic = picBuilder.build();
			picList.add(pic);
		}
		builder.addAllProductPicList(picList);
		
		//生成Product对象
		ProductInfoPro.Product product = builder.build();
		
		//转换为byte
		byte[] products = product.toByteArray();
		
		//反解
		try {
			productParse = ProductInfoPro.Product.parseFrom(products);
		} catch (InvalidProtocolBufferException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
	}
	
	
}
